﻿@using hc.epm.UI.Common;
@using hc.epm.DataModel.Basic;
@model Base_User
@{
    ViewBag.Title = "会员添加";
    Layout = "~/Views/Shared/_LayoutHc.cshtml";
}
<style>
    .btn-group {
        margin-top: 0px !important;
        text-align: left !important;
    }
</style>
<div class="container">
    <div class="row">
        <form class="form-horizontal " id="mainForm">
            <div class="col-lg-12 col-sm-12" style="margin-bottom:60px">
                <input type="hidden" name="PassTime" value="@ViewBag.currentTime" />
                <input type="hidden" name="LastLoginTime" value="@ViewBag.currentTime" />
                <input type="hidden" name="Belong" value="@ViewBag.Belong" />
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>用户编码 : </label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="UserCode" class="form-control" id="UserCode" placeholder="请输入编码" value="" />
                    </div>
                </div>
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>用户名 : </label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="UserName" class="form-control" id="UserName" placeholder="请输入用户名" value="" />
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12" style="margin-bottom:60px">
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>性别：</label>
                    <div class="col-lg-8 col-sm-8" style="margin-left:0px;">
                        <div class="radio radio-success radio-inline">
                            <input type="radio" id="inlineRadio1" value=true name="Sex" checked>
                            <label for="inlineRadio1"> 男 </label>
                        </div>
                        <div class="radio radio-success radio-inline">
                            <input type="radio" id="inlineRadio2" value=false name="Sex">
                            <label for="inlineRadio2">女  </label>
                        </div>
                    </div>
                </div>
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>登录账号：</label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="UserAcct" id="UserAcct" class="form-control" placeholder="请输入登录账号" />
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12" style="margin-bottom:60px">
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>联系电话：</label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="Phone" id="Phone" placeholder="请输入联系电话" class="form-control" value="" />
                    </div>
                </div>
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>电子邮箱：</label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="Email" id="Email" placeholder="请输入电子邮箱" class="form-control" value="" />
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12" style="margin-bottom:60px">
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label">微信：</label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="WebChat" id="WebChat" placeholder="请输入微信" autocomplete="off" class="form-control" />
                    </div>
                </div>
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label">QQ：</label>
                    <div class="col-lg-8 col-sm-8">
                        <input type="text" name="QQ" id="QQ" placeholder="请输入QQ" autocomplete="off" class="form-control" />
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12" style="margin-bottom:60px">
                <div class="form-group col-lg-6 col-sm-6">
                    <label class="col-lg-4 col-sm-4 control-label"><em class="required">*</em>所属单位 : </label>
                    <div class="col-lg-8 col-sm-8">
                        @Html.DropDownList("CompanyId", null, new { @class = "form-control selectpicker  show-tick" })
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12" style="height:110px">
                <div class="form-group">
                    <label class="col-lg-2 col-sm-2 control-label"><em class="required">*</em>图像：</label>
                    <div class="col-lg-6 col-sm-6">
                        <div style="width:100%;" id="fileList">
                        </div>
                        <div class="layui-block col-sm-5">
                            <div style="width:100%;display:none;margin-left:120px;" id="fileBar">
                                <div class="layui-progress" lay-showpercent="true" lay-filter="fileProgress"><div class="layui-progress-bar" lay-percent="0%" style="width: 0%;"><span class="layui-progress-text">NaN%</span></div></div>
                            </div>
                            <input type="hidden" name="fileDataJson" id="fileDataJson">
                        </div>
                        <div class="layui-form-item ">
                            <div class="layui-form-item menue">
                                <div class="layui-inline col-sm-6" style="padding-left:0px; padding-top:10px;">
                                    <div class="layui-input-inline">
                                        <button class="layui-btn layui-btn-primary " id="btnSelect" style="position: relative; z-index: 1;">
                                            <i class="layui-icon" style="font-size:30px;"></i>
                                        </button>
                                        <button class="layui-btn layui-btn-primary " id="btnUpload" style="margin:0">
                                            <i class="layui-icon" style="font-size:30px;"></i>
                                        </button>
                                        <div id="html5_1cc562u8f6oj148f1162ngkih33_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 0px; width: 71px; height: 38px; overflow: hidden; z-index: 0;">
                                            <input id="html5_1cc562u8f6oj148f1162ngkih33" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" accept="image/jpeg,image/gif,image/png,application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item row btn-save" style="margin-top:60px">
                <input type="button" name="save" id="save" lay-submit lay-filter="sub" value="保&nbsp;&nbsp;&nbsp;存" class="layui-btn layui-btn-small button-base bg-save" />
                <input type="button" name="close" value="关&nbsp;&nbsp;&nbsp;闭" class="layui-btn layui-btn-small button-base bg-cancel cancel" />
            </div>
        </form>
    </div>
</div>

<script>
    //自定义验证规则
    window.onload = function () {
        $('#mainForm').bootstrapValidator({
            excluded: [':hidden'],//[':disabled', ':hidden', ':not(:visible)'] //设置隐藏组件可验证
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                UserName: {
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        }, stringLength: {
                            max: 50,
                            message: '用户名长度不能超过50字符'
                        },
                    }
                },
                UserAcct: {
                    validators: {
                        notEmpty: {
                            message: '登录账号不能为空'
                        },
                        regexp: {
                            regexp: /^[A-Za-z][A-Za-z0-9_-]{5,19}$/,
                            message: "以字母开头的6-20位字母、数字‘-’或‘_’组成"
                        }, stringLength: {
                            max: 100,
                            message: '登录账号长度不能超过100字符'
                        },
                    }
                },
                UserCode: {
                    validators: {
                        notEmpty: {
                            message: '用户编码不能为空'
                        }, stringLength: {
                            max: 50,
                            message: '用户编码长度不能超过50字符'
                        },
                    }
                },
                Phone: {
                    validators: {
                        notEmpty: {
                            message: "联系电话不能为空"
                        },
                        regexp: {
                            regexp: /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,
                            message: "联系电话格式有误"
                        }
                    }
                },
                Email: {
                    validators: {
                        notEmpty: {
                            message: "邮箱不能为空"
                        },
                        emailAddress: {
                            message: '邮箱格式不正确'
                        }, stringLength: {
                            max: 100,
                            message: '邮箱长度不能超过100字符'
                        },
                    }
                },
                WebChat: {
                    validators: {
                        stringLength: {
                            max: 50,
                            message: '微信号长度不能超过50字符'
                        },
                    }
                },
                QQ: {
                    validators: {
                        digits: {
                            message: 'QQ号码只能是数字'
                        }, stringLength: {
                            max: 50,
                            message: 'QQ号码长度不能超过50字符'
                        },
                    }
                },
                CompanyId: {
                    validators: {
                        notEmpty: {
                            message: "所属单位不能为空"
                        },
                    }
                },
            }
        });
    }

    layui.use(['layer', 'form', 'element'], function () {
        var layer = layui.layer, form = layui.form, element = layui.element, laypage = layui.laypage;

        //图标上传
        var uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4', // 这里是说用什么技术引擎
            url: FileResourceURL, // 服务端上传路径
            max_file_size: MaxPicSize, // 文件上传最大限制。
            max_file_count: 1, //指示用户可以同时上传文件的最大数量
            chunk_size: 0, // 上传分块每块的大小，这个值小于服务器最大上传限制的值即可,0表示不启用分块。
            browse_button: 'btnSelect',//触发上传元素
            flash_swf_url: '/Scripts/plupload_2_1_2/Moxie.swf',
            silverlight_xap_url: '/Scripts/plupload_2_1_2/Moxie.xap',
            multi_selection: false,//	是否可以在文件浏览对话框中选择多个文件
            //附加参数
            multipart_params: {
                app: 'epm',
                crop: true
            },
            filters: {
                mime_types: [ //只允许上传图片和zip文件
                { title: "Image files", extensions: "jpg,png,jpeg,gif" }
                ],
                max_file_size: MaxPicSize, //最大只能上传400kb的文件
                prevent_duplicates: true //不允许选取重复文件
            },
            rename: false, // 是否重命名文件
            init: {
                PostInit: function () {
                    $('#btnUpload').click(function () {
                        if (uploader.files.length == 0) {
                            layer.alert("请选择要上传的附件", { icon: 2 });
                        } else {
                            uploader.start();
                            return false;
                        }
                    });
                },
                FilesAdded: function (up, files) {
                    var type;
                    plupload.each(files, function (file) {
                        var typeList = ["jpg", "png", "jpeg", "gif"];
                        var fileType = File(typeList, file);
                        if (fileType) {
                            $('#fileList').html("");
                            //限定只能上传一个文件
                            if (files.length > 1) {
                                up.removeFile(files[0]);
                            }
                            plupload.each(files, function (file) {
                                $('#fileList').append('<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>');
                            });
                        } else {
                            files.pop();
                            layer.alert('上传文件类型仅支持"jpeg","jpg","png","gif"格式，请重新上传', { icon: 2 });
                        }
                    });
                },
                UploadProgress: function (up, file) {
                    $("#fileBar").show();
                    element.progress("fileProgress", file.percent + "%");
                },
                FileUploaded: function (uploader, file, responseObject) {
                    var data = JSON.parse(responseObject.response);
                    if (data.file) {
                        fileDataArray = [];
                        for (var i = 0; i < data.file.length; i++) {

                            var imgtype = getImageType(data.file[i].Name, data.file[i].Extension);
                            var imgurl = FileURL + data.file[i].VirtualURL;

                            if (!isThumbnailImage(imgtype)) {
                                var odiv = '<img src="' + imgurl + '" alt="Logo" id="ImgShow" style="width: 100px;height: 100px;" />';
                                $('#fileList').html(odiv);
                            }

                            setTimeout(function () {
                                $("#fileBar").hide();
                                element.progress("fileProgress", "0%");
                            }, 1500);

                            if (!data.flag) {
                                layer.alert("错误,请检查文件格式后重新上传！");
                                return;
                            }
                            layer.msg("附件上传成功", { time: 1000, icon: 1 });
                            uploader.removeFile(file);

                            var obj = {};
                            obj.GuidId = file.id;
                            obj.Name = data.file[i].Name;
                            obj.Size = data.file[i].FileSize;
                            obj.FileGuid = data.file[i].Guid;
                            obj.Url = imgurl;
                            obj.ImageType = imgtype;

                            fileDataArray.push(obj);
                        }
                        fileDataJson = JSON.stringify(fileDataArray);
                        $("#fileDataJson").val(fileDataJson);
                    }
                },
                Error: function (up, err) {
                    if (err.code == -600) {
                        layer.alert('仅支持' + MaxPicSize + '以内文件，请重新上传', { icon: 2 })
                    }
                    else if (err.code == -602) {
                        layer.alert('不允许上传重复文件，请重新上传', { icon: 2 })
                    }
                    else {
                        layer.alert("错误：" + err.code + ": " + err.message);
                    }
                }
            }
        });
        uploader.init();

        //监听提交
        form.on('submit(sub)', function (data) {
            //对表单进行验证
            var bv = $('#mainForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                if ($("#fileDataJson").val() == '') {
                    layer.alert("用户头像不能为空", { icon: 2 })
                }
                var search = $("form").serialize();
                $.post("Add", search, function (data) {
                    if (data.Flag) {
                        $("#save").attr("disabled", "disabled");
                        $("#save").val("请等待")
                        var layerIndex = layer.msg("会员添加成功", { time: 1000, icon: 1 }, function () {
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);//关闭窗口
                            parent.location.reload();
                        });
                    } else {
                        layer.alert(data.Message, { icon: 2 });
                    }
                });
            }
            return false;
        });

        //关闭弹出iframe层
        $('.cancel').on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);//关闭窗口
        });
    });
</script>
